<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <style>
    html,
    body,
    #container {
        width: 100%;
        height: 100%;
    }
    #panel {
        position: fixed;
        background-color: white;
        max-height: 90%;
        overflow-y: auto;
        top: 10px;
        right: 10px;
        width: 280px;
    }
    </style>
    <title>位置经纬度 + 获取驾车规划数据</title>
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
    <script src="https://webapi.amap.com/maps?v=1.4.10&key=f684c045b74d259d9c2cb4b10c320fab&plugin=AMap.Driving"></script>
    <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
    <script src="./js/common.js"></script>
    <script src="./lib/jquery.js"></script>
</head>
<body>
<div id="container"></div>
<div id="panel"></div>
<script type="text/javascript">
	
	var targetList = [];
    var resultCount = 0;
    
    var userid = 1;		 //用户编号
	
    var map = new AMap.Map("container", {
        center: [116.397559, 39.89621],
        zoom: 14
    });
    
    getNoTaskList();
    
	//获取未完成任务列表
	function getNoTaskList(){
		var jsonObj = {
				"userid":userid
		};
		var jsonStr = json2str(jsonObj);
		callAjax("getNoTaskList", jsonStr, "获取未完成任务列表:getNoTaskList",function(result){
			debugger;
		});
	}
    
    function navigationPlan(){
    	
	    
	    //我的位置
	    var currentLocation = {lng:116.337612,lat:39.926977,name:"我的位置", address:"北京市西城区展览路街道扣钟胡同中国地质科学院"};
	    var startPoint = new AMap.LngLat(currentLocation.lng, currentLocation.lat);
	    //送货点列表
	    targetList = [
	    	{lng:116.378596,lat:39.865208,name:"北京南站", address:"北京市丰台区右安门街道北京南站"},
	    	{lng:116.321583,lat:39.895012,name:"北京西站", address:"北京市丰台区太平桥街道广莲路北京西站"},
	    	{lng:116.430416,lat:39.902387,name:"北京站", address:"北京市东城区建国门街道北京站"},
	    	{lng:116.351538,lat:39.945106,name:"北京北站", address:"北京市西城区展览路街道北京北站"},
	    ];
	
		for(var i=0; i<targetList.length; i++){
			var targetPoint = targetList[i];
			var endPoint = new AMap.LngLat(targetPoint.lng, targetPoint.lat); 
			drivingSearch(startPoint, endPoint, targetPoint);
		}
    }
    
    function drivingSearch(startPoint, endPoint, data){
    	
    	 // 构造路线导航类
	    var driving = new AMap.Driving({
	        policy: AMap.DrivingPolicy.LEAST_TIME, // 其它policy参数请参考 https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingPolicy
	        ferry: 1, // 是否可以使用轮渡
	        province: '京', // 车牌省份的汉字缩写 
//	        map: map,
//	        panel: 'panel'
	    });
    	
    	// 根据起终点经纬度规划驾车导航路线
    	driving.search(startPoint, endPoint, function(status, result){
	    	// result 即是对应的驾车导航信息，相关数据结构文档请参考  https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingResult
	        if (status === 'complete') {
	            log.success('获取驾车数据成功')
	            data.distance = result.routes[0].distance;
	            data.time = result.routes[0].time
	        } else {
	            log.error('获取驾车数据失败：' + result)
	        }
	        resultCount++;
	        if(resultCount == targetList.length){
	        	onSearchEnd();
	        }
    	});
    }
    
    function onSearchEnd(){
    	debugger;
    	var timeList = maopao(targetList, "time");
    	var distanceList = maopao(targetList, "distance");
    	debugger;
    	function maopao(targetList, attri){
    		var arra = targetList.concat();
	        var temp;
	        for(var i=0;i<arra.length;i++){ 
	            for(var j=0;j<arra.length-i-1;j++){
	                if(arra[j][attri]>arra[j+1][attri]){
	                    temp=arra[j];
	                    arra[j]=arra[j+1];
	                    arra[j+1]=temp;
	                }
	            }
	        };
	    	return arra;
	    }
    }
    
        //调用后台接口
	function callAjax(funcName, paramObj, log, callback){
		$.ajax({
			type: "post",
			url: fxyjBackUrl + "Address" + funcName,
			data: paramObj,
			contentType:'application/json',
			dataType: 'json',
			success: function(result) {
				console.log(log);
				console.log(result);
				if(callback){
					callback(result);
				}
				}
			});
	}


   
</script>
</body>
</html>